﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WFFace.utils;

namespace WFFace
{
    public partial class VideoForm : Form
    {

        //创建/删除的文件路径和文件名
        private readonly string fileDirectory = "path/to/directory"; //文件夹/目录
        private readonly string fileName = "path/to/directory/file.txt"; //文件名

        public VideoForm()
        {
            InitializeComponent();
            // 设置窗口的起始位置为屏幕中央
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "C#开发应掌握的处理文件系统I/O知识点";//窗口标题
        }

        //创建文件,后写入内容

        private void BtnCreate_Click(object sender, EventArgs e)
        {

            // File.Create("path/to/directory/file.txt"); 创建文件未关闭进程
            //如果创建完文件后不关闭创建进程则会报错： C#使用Create创建文件后，报The Process cannot access the file because it is being used by another process的异常
            //如果不Close(),那么这个文件一直被创建进程占着，直到创建进程被关闭

            //如果文件不存在则创建文件
            if (!File.Exists(fileName))
            {
                File.Create(fileName).Close(); //创建文件并关闭进程
            }


            //写入内容
            //using (StreamWriter writer = new StreamWriter(fileName))
            using StreamWriter writer = new(fileName);
            writer.WriteLine("这是新建的文件，我写入了内容了！");

        }

        //读取文件内容
        private void BtnRead_Click(object sender, EventArgs e)
        {
            using StreamReader reader = new(fileName);
            string content = reader.ReadToEnd();
            //Console.WriteLine(content);
            MessageBox.Show(content);
        }

        //删除文件
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            //异常捕抓  在进行文件系统I/O操作时，处理可能发生的异常是不可或缺的。
            //可能的异常包括文件不存在、权限不足等。使用try-catch块来捕获这些异常，以确保应用程序的稳定性。
            try
            {
                File.Delete(fileName);
            }
            catch (IOException ex)
            {
                //Console.WriteLine(ex.Message);
                MessageBox.Show($"Error: {ex.Message}");
            }
            catch (UnauthorizedAccessException ex)
            {
                //Console.WriteLine($"{ex.Message}");
                MessageBox.Show($"{ex.Message}");
            }

        }

        //创建文件夹/目录
        private void BtnCreateDirector_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory(fileDirectory);
        }

        //删除文件夹/目录
        private void BtnDeleteDirector_Click(object sender, EventArgs e)
        {
            Directory.Delete(fileDirectory, true);
        }

        //屏幕截图
        private void BtnScreenShot_Click(object sender, EventArgs e)
        {
            //获取屏幕大小
            Rectangle bounds = Screen.GetBounds(Point.Empty);

            //创建一个bitmap对象并将其设置为屏幕大小
            using (Bitmap bitmap = new(bounds.Width, bounds.Height))
            {

                //创建一个Graphics对象并将其设置为bitmap
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {

                    //将整个屏幕绘制到Graphics对象中
                    graphics.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }

                //文件是否存在
                if (!Directory.Exists("path/to/screenshot"))
                {

                    //文件目录不存在则创建
                    Directory.CreateDirectory("path/to/screenshot");
                }

                //将截图保存为jpg文件
                bitmap.Save("path/to/screenshot/image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            MessageBox.Show("屏幕截图已保存");

        }

        //指定范围的随机数
        private void BtnRandom_Click(object sender, EventArgs e)
        {
            int[] selectNum = Enumerable.Range(1, 12).OrderBy(t => Guid.NewGuid()).Take(4).ToArray();

            MessageBox.Show(selectNum.ToString());
        }

        private void VideoForm_Load(object sender, EventArgs e)
        {

            //普通数组{1,2,3,4,5,6}
            //初始化 清除项
            listBoxOne.Items.Clear();
            listBoxOne.Items.Add("zero-item");//
            listBoxOne.Items.Add("one-item");
            string[] list = { "two-item", "three-item", "four-item", "five-item" };
            listBoxOne.Items.AddRange(list);

            //多维数组 {{id:1,name:zs},{id:2,name:ls}}
            //int[,] value = { { } };
            //int[,] twoList = value;

        }

        //分享图片
        private void BtnShareImage_Click(object sender, EventArgs e)
        {
            using var ms = new MemoryStream();
            Image image = Image.FromFile("path/to/screenshot/image.jpg");
            image.Save(ms, ImageFormat.Jpeg);
            byte[] bytes = ms.ToArray();
        }

        //去到首页
        private void BtnForm1_Click(object sender, EventArgs e)
        {
            MyCommon.WindowNavNewView(new Form1()); //引用方法
            Close();  //关闭当前窗口
            //Dispose(); //释放资源
        }


        //去到分享页
        private void BtnShareView_Click(object sender, EventArgs e)
        {
            MyCommon.WindowNavNewView(new ShareForm()); //引用方法
            Close();  //关闭当前窗口
            //Dispose(); //释放资源
        }


        //录屏
        private void BtnScreenCaptrue_Click(object sender, EventArgs e)
        {
            var img = MyCommon.CaptureScreen(); //获取图片路径/结果返回文件路径

            // pictureBox1.Image=Image.FromFile(img); //直接读取文件路径 ，导致图片被占用（无法替换和修改图片）且内存增大，此方法不建议使用

            FileStream fileStream = new FileStream(img, FileMode.Open, FileAccess.Read); //转为文件流
            pictureBox1.Image = Image.FromStream(fileStream); //读取文件流
            fileStream.Close();
            fileStream.Dispose();
            GC.Collect();//回收GC释放的资源,不回收的话每次绘图都会增加内存
        }
    }
}
